/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "status.h"
#include <mc/log.h>

namespace dev {

bool Gpu_Status::read_ECCModeEnabled(postbox_ptr postbox) {
    if (!postbox) {
        elog("Gpu_Status.read_ECCModeEnabled: postbox not initialized");
        return false;
    }
    try {
        SMBusPostboxProtocol::RequestParams params;
        params.name           = "CardECCModeEnabled";
        params.opcode         = 0x18;
        params.arg1           = 0x00;
        params.arg2           = 0x00;
        params.capa_dword_num = 1;
        params.capa_mask      = 0x00800000;
        params.data_out       = true;
        params.data_len       = 1;
        params.data_type      = SMBusPostboxProtocol::KEEP_DIGITAL;
        params.data_mask      = 0x01;

        std::string result_str = postbox->sendRequest(params);
        if (result_str.empty()) {
            elog("Gpu_Status.read_ECCModeEnabled: empty response");
            return false;
        }
        elog("Gpu_Status.CardECCModeEnabled read: {CardECCModeEnabled}",
             ("CardECCModeEnabled", result_str));
        int32_t raw_value = std::stoi(result_str);
        ECCModeEnabled.set_value(static_cast<uint8_t>(raw_value & 0xFF)); // UINT8
        return true;
    } catch (const std::exception& e) {
        elog("Gpu_Status.read_ECCModeEnabled: exception=${err}", ("err", e.what()));
        return false;
    }
}

bool Gpu_Status::read_ECCModePendingEnabled(postbox_ptr postbox) {
    if (!postbox) {
        elog("Gpu_Status.read_ECCModePendingEnabled: postbox not initialized");
        return false;
    }
    try {
        SMBusPostboxProtocol::RequestParams params;
        params.name           = "CardECCModePendingEnabled";
        params.opcode         = 0x18;
        params.arg1           = 0x00;
        params.arg2           = 0x00;
        params.capa_dword_num = 1;
        params.capa_mask      = 0x00800000;
        params.data_out       = true;
        params.data_len       = 1;
        params.data_type      = SMBusPostboxProtocol::KEEP_DIGITAL;
        params.data_mask      = 0x04;

        std::string result_str = postbox->sendRequest(params);
        if (result_str.empty()) {
            elog("Gpu_Status.read_ECCModePendingEnabled: empty response");
            return false;
        }
        elog("Gpu_Status.CardECCModePendingEnabled read: {CardECCModePendingEnabled}",
             ("CardECCModePendingEnabled", result_str));
        int32_t raw_value = std::stoi(result_str);
        ECCModePendingEnabled.set_value(static_cast<uint8_t>(raw_value & 0xFF)); // UINT8
        return true;
    } catch (const std::exception& e) {
        elog("Gpu_Status.read_ECCModePendingEnabled: exception=${err}", ("err", e.what()));
        return false;
    }
}

bool Gpu_Status::read_resetRequired(postbox_ptr postbox) {
    if (!postbox) {
        elog("Gpu_Status.read_resetRequired: postbox not initialized");
        return false;
    }
    try {
        SMBusPostboxProtocol::RequestParams params;
        params.name           = "CardGPUResetRequired";
        params.opcode         = 0x18;
        params.arg1           = 0x01;
        params.arg2           = 0x00;
        params.capa_dword_num = 1;
        params.capa_mask      = 0x01000000;
        params.data_out       = true;
        params.data_len       = 1;
        params.data_type      = SMBusPostboxProtocol::KEEP_DIGITAL;
        params.data_mask      = 0x01;

        std::string result_str = postbox->sendRequest(params);
        if (result_str.empty()) {
            elog("Gpu_Status.read_resetRequired: empty response");
            return false;
        }
        elog("Gpu_Status.CardGPUResetRequired read: {CardGPUResetRequired}",
             ("CardGPUResetRequired", result_str));
        int32_t raw_value = std::stoi(result_str);
        ResetRequired.set_value(static_cast<uint8_t>(raw_value & 0xFF));
        return true;
    } catch (const std::exception& e) {
        elog("Gpu_Status.read_resetRequired: exception=${err}", ("err", e.what()));
        return false;
    }
}

} // namespace dev

MC_REFLECT(dev::Gpu_Status)
